<?php

include_once $_SERVER['DOCUMENT_ROOT']."/controller/connect.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/Task.php";
/**
 * Created by PhpStorm.
 * User: lwh
 * Date: 2017/7/20
 * Time: 19:46
 */
class TaskDAO{
    const  FIELD_ID = "taskId";
    const  FIELD_TEMPLATE_ID = "templateId";
    const  FIELD_USER_ID = "userId";
    const  FIELD_TITLE = "title";
    const  FIELD_DESCRIPTION = "description";
    const  FIELD_STATUS = "taskStatus";
    const  FIELD_CURRENT_STAGE = "currentStage";
    const  FIELD_BONUS_REWARD = "bonus";
    const  FIELD_PUBLISH_TIME = "publish_time";
    const  FIELD_DEADLINE = "ddl";
    const  FIELD_USER_TYPE = "userScope";

    public $conn;

    function __construct(){
        $this->conn = new db();
    }

    //插入任务
    public function insert($task){

    }

    //删除任务
    public function delete($taskId){

    }

    //查询任务
    public function query($taskId){
        $sql = "select * from task where ".self::FIELD_ID." = ".$taskId;
        $db = $this->conn->getDBConn();
        $row = $db->query($sql)->fetch(PDO::FETCH_ASSOC);//调用查询函数query(),并以关联数组的形式储存
        if($row){
            $task = $this->loadDataFromResult($row);
            return $task;
        }
        else
            return null;
    }

    //更新任务
    public function update($task){
        if($this->query($task->getId()) == null)
            return -1;
        //更新操作
        $db = $this->conn->getDBConn();
        $stmt = $db->prepare("update task set ".self::FIELD_TITLE." = ?, ".self::FIELD_DESCRIPTION." = ?, ".self::FIELD_STATUS." = ?, "
            .self::FIELD_CURRENT_STAGE." = ?, ".self::FIELD_BONUS_REWARD." = ?, ".self::FIELD_PUBLISH_TIME." = ?, ".self::FIELD_DEADLINE." = ?, ".self::FIELD_USER_TYPE." = ? where ".self::FIELD_ID." = ?");
        $stmt->bindParam(1,$task->getTitle());
        $stmt->bindParam(2,$task->getDescription());
        $stmt->bindParam(3,$task->getStatus());
        $stmt->bindParam(4,$task->getCurrentStage());
        $stmt->bindParam(5,$task->getBonusReward());
        $stmt->bindParam(6,$task->getPublishTime());
        $stmt->bindParam(7,$task->getDeadline());
        $stmt->bindParam(8,$task->getUserType());
        $stmt->bindParam(9,$task->getId());
        $result = $stmt->execute();
		//var_dump($stmt->errorInfo());echo "<br />";
        return $result;
    }

    //查询所有任务列表
    public function queryAllTask(){
        $sql = "select * from task";
        $db = $this->conn->getDBConn();
        $rows = $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);//调用查询函数query(),并以关联数组的形式储存
        if(count($rows) > 0){
            $tasks = array();
            foreach ($rows as $row){
                $task = $this->loadDataFromResult($row);
                array_push($tasks,$task);
            }
            return $tasks;
        }
        else
            return null;
    }

    //查询用户发布的任务
    public function queryTaskListByUser($userId){
        $sql = "select * from task where ".self::FIELD_USER_ID." = ".$userId;
        $db = $this->conn->getDBConn();
        $rows = $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);//调用查询函数query(),并以关联数组的形式储存
        if(count($rows) > 0){
            $tasks = array();
            foreach ($rows as $row){
                $task = $this->loadDataFromResult($row);
                array_push($tasks,$task);
            }
            return $tasks;
        }
        else
            return null;
    }

    //从结果集中构建出task对象
    private function loadDataFromResult($row){
        $task = new Task();
        $task->setId($row[self::FIELD_ID]);
        $task->setTemplateId($row[self::FIELD_TEMPLATE_ID]);
        $task->setUserId($row[self::FIELD_USER_ID]);
        $task->setTitle($row[self::FIELD_TITLE]);
        $task->setDescription($row[self::FIELD_DESCRIPTION]);
        $task->setStatus($row[self::FIELD_STATUS]);
        $task->setCurrentStage($row[self::FIELD_CURRENT_STAGE]);
        $task->setBonusReward($row[self::FIELD_BONUS_REWARD]);
        $task->setDeadline($row[self::FIELD_DEADLINE]);
        $task->setPublishTime($row[self::FIELD_PUBLISH_TIME]);
        $task->setUserType($row[self::FIELD_USER_TYPE]);
        return $task;
    }
}
